package com.billy.ohos.swipe.demo.consumer;

import com.billy.ohos.swipe.SmartSwipe;
import com.billy.ohos.swipe.consumer.AbilitySlidingBackConsumer;
import com.billy.ohos.swipe.demo.BaseAbility;
import com.billy.ohos.swipe.demo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;

import java.text.DecimalFormat;
import java.text.NumberFormat;


/**
 * demo:
 * @author billy.qi
 */
public class SwipeBackTranslucentConsumerAbility extends BaseAbility {

    private NumberFormat formatter = new DecimalFormat("0.00");

    private Text numberTextView;
    private AbilitySlidingBackConsumer consumer;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer parse = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_with_description, null, false);
        super.setUIContent(parse);
        Text textView = (Text) findComponentById(ResourceTable.Id_description);
        textView.setText(ResourceTable.String_demo_description_SwipeBackTranslucentConsumer);
        consumer = SmartSwipe.wrap(parse)
                .removeAllConsumers()
                .addConsumer(new AbilitySlidingBackConsumer(this))
                .setRelativeMoveFactor(0.5F)
                .enableAllDirections()
                .as(AbilitySlidingBackConsumer.class);
        Text label = new Text(this);
        label.setText(ResourceTable.String_demo_main_group_main_ui_relative_move_factor);
        label.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        label.setTextSize(40);
        parse.addComponent(label);
        numberTextView = new Text(this);
        numberTextView.setTextColor(new Color(getColor(ResourceTable.Color_colorPrimary)));
        numberTextView.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        numberTextView.setTextSize(50);
        parse.addComponent(numberTextView);
        Slider seekBar = new Slider(this);
        seekBar.setWidth(AttrHelper.vp2px(340 , this));
        ShapeElement shapeElement = new ShapeElement(this , ResourceTable.Graphic_shape_circle_slider);
        seekBar.setThumbElement(shapeElement);
        seekBar.setMaxValue(100);
        seekBar.setProgressValue(50);
        numberTextView.setText(formatter.format(50 * 0.01F));
        seekBar.setProgressColor(new Color(Color.getIntColor("#008577")));
        seekBar.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean b) {
//                float moveFactor = consumer.setRelativeMoveFactor(progress * 0.01F).getRelativeMoveFactor();
                numberTextView.setText(formatter.format(progress * 0.01F));
            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {

            }
        });
        parse.addComponent(seekBar);
    }


    @Override
    public int getTitleResId() {
        return ResourceTable.String_demo_ui_AbilitySlidingBackConsumer;
    }
}
